﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UCL.Forms.Components
{
    /// <summary>
    /// 滚动消息面板
    /// </summary>
    /// <remarks>2014-3-22 by sutroon</remarks>
    public partial class ucScrollPanel : UserControl
    {
        /// <summary>
        /// 获取或设置滚动方向
        /// <para>UP|DOWN|LEFT|RIGHT</para>
        /// </summary>
        public string ScrollingDirection { get; set; }
        public ucScrollPanel()
        {
            InitializeComponent();
        }
        public IList<string> GetTestData()
        {
            IList<string> lst = new List<string>();
            int i = 0;
            while (i < 20)
            {
                lst.Add("这里是滚动的图片或文字" + i);
                i++;
            }
            return lst;
        }
        public void LoadData(IList<string> data, string direct = "")
        {
            if (string.IsNullOrEmpty(direct)) direct = this.ScrollingDirection;
            StringBuilder sb = new StringBuilder();
            sb.Append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            sb.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
            sb.Append("<head>");
            sb.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            sb.Append("<title></title><style type='text/css'>body{font-size:12px;margin:0px;}td{white-space:nowrap}</style>");
            sb.Append("</head>");
            sb.Append("<body>");
            switch (direct)
            {
                case "UP":
                    sb.Append(string.Format("<div id='colee'style='overflow: hidden; height: 153px; width: 410px;'><div id='colee1'>{0}</div><div id='colee2'></div></div>", data.ToString("", "<p>{0}</p>")));
                    sb.Append("<script>var speed=30;var colee2=document.getElementById('colee2');var colee1=document.getElementById('colee1');var colee=document.getElementById('colee');colee2.innerHTML=colee1.innerHTML;function Marquee1(){if(colee2.offsetTop-colee.scrollTop<=0){colee.scrollTop-=colee1.offsetHeight}else{colee.scrollTop++}};var MyMar1=setInterval(Marquee1,speed);colee.onmouseover=function(){clearInterval(MyMar1)};colee.onmouseout=function(){MyMar1=setInterval(Marquee1,speed)}</script>");
                    break;
                case "DOWN":
                    sb.Append(string.Format("<div id='colee_bottom'style='overflow: hidden; height: 253px; width: 410px;'><div id='colee_bottom1'>{0}</div><div id='colee_bottom2'></div></div>", data.ToString("", "<p>{0}</p>")));
                    sb.Append("<script>var speed=30;var colee_bottom2=document.getElementById('colee_bottom2');var colee_bottom1=document.getElementById('colee_bottom');var colee_bottom=document.getElementById('colee_bottom');colee_bottom2.innerHTML=colee_bottom1.innerHTML;colee_bottom.scrollTop=colee_bottom.scrollHeight;function Marquee2(){if(colee_bottom1.offsetTop-colee_bottom.scrollTop>=0){colee_bottom.scrollTop+=colee_bottom2.offsetHeight}else{colee_bottom.scrollTop--}};var MyMar2=setInterval(Marquee2,speed);colee_bottom.onmouseover=function(){clearInterval(MyMar2)};colee_bottom.onmouseout=function(){MyMar2=setInterval(Marquee2,speed)}</script>");
                    break;
                case "LEFT":
                    sb.Append(string.Format("<div id='colee_left'style='overflow: hidden; width: 100px;'><table cellpadding='0'cellspacing='0'border='0'><tr><td id='colee_left1'valign='top'align='center'><table cellpadding='2'cellspacing='0'border='0'><tr align='center'>{0}</tr></table></td><td id='colee_left2'valign='top'></td></tr></table></div>", data.ToString("", "<td><p>{0}</p></td>")));
                    sb.Append("<script>var speed=30;var colee_left2=document.getElementById('colee_left2');var colee_left1=document.getElementById('colee_left1');var colee_left=document.getElementById('colee_left');colee_left2.innerHTML=colee_left1.innerHTML;function Marquee3(){if(colee_left2.offsetWidth-colee_left.scrollLeft<=0){colee_left.scrollLeft-=colee_left1.offsetWidth;}else{colee_left.scrollLeft++}};var MyMar3=setInterval(Marquee3,speed);colee_left.onmouseover=function(){clearInterval(MyMar3)};colee_left.onmouseout=function(){MyMar3=setInterval(Marquee3,speed)}</script>");
                    break;
                case "RIGHT":
                    sb.Append(string.Format("<div id='colee_right'style='overflow: hidden; width: 500px;'><table cellpadding='0'cellspacing='0'border='0'><tr><td id='colee_right1'valign='top'align='center'><table cellpadding='2'cellspacing='0'border='0'><tr align='center'>{0}</tr></table></td><td id='colee_right2'valign='top'></td></tr></table></div>", data.ToString("", "<td><p>{0}</p></td>")));
                    sb.Append("<script>var speed=30;var colee_right2=document.getElementById('colee_right2');var colee_right1=document.getElementById('colee_right1');var colee_right=document.getElementById('colee_right');colee_right2.innerHTML=colee_right1.innerHTML;function Marquee4(){if(colee_right.scrollLeft<=0){colee_right.scrollLeft+=colee_right2.offsetWidth}else{colee_right.scrollLeft--}};var MyMar4=setInterval(Marquee4,speed);colee_right.onmouseover=function(){clearInterval(MyMar4)};colee_right.onmouseout=function(){MyMar4=setInterval(Marquee4,speed)}</script>");
                    break;
            }
            sb.Append("</body></html>");
            wbMain.DocumentText = sb.ToString();
        }
    }
}
